﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;

namespace AutomationFoundation.CodeToolKit.VisualStudio
{
    internal static class ProjectHelper
    {
        /// <summary>
        /// Gets the value from a project property.
        /// </summary>
        /// <param name="projectInfo">The project to search</param>
        /// <param name="propertyKey">The property key to search for. This is case insensitive.</param>
        /// <returns>The value of property or a empty string.</returns>
        public static string GetPropertyValue(this Project projectInfo, string propertyKey)
        {
            var resultData = from p in projectInfo.Properties.Cast<Property>()
                             where string.Equals(p.Name, propertyKey, StringComparison.InvariantCultureIgnoreCase)
                             select p.Value;

            string value = resultData.SingleOrDefault() as string;

            if (value != null)
            {
                return value;
            }

            return string.Empty;
        }

        /// <summary>
        /// Gets the value from a project property.
        /// </summary>
        /// <param name="projectInfo">The project to search</param>
        /// <typeparam name="T"> The expected return type.</typeparam>
        /// <param name="propertyKey">The property key to search for. This is case insensitive.</param>
        /// <returns>The value of property or a default value for the type.</returns>
        public static T GetPropertyValue<T>(this Project projectInfo, string propertyKey)
        {
            var resultData = from p in projectInfo.Properties.Cast<Property>()
                             where string.Equals(p.Name, propertyKey, StringComparison.InvariantCultureIgnoreCase)
                             select p.Value;

            T value = default(T);
            try
            {
                value = (T)resultData.SingleOrDefault();
            }

            catch (System.Exception)
            {
                //swallow the exception and return a default value.
                return default(T);
            }
            return value;
        }

        /// <summary>
        /// Determines if a file exists within a project.
        /// </summary>
        /// <param name="projectInfo">The visual studio project to search</param>
        /// <param name="filePath">The full file path of the file to search for.</param>
        /// <returns>Boolean flag true if the file is found, or false if the file cannot be found.</returns>
        public static bool FileExists(this Project projectInfo, string filePath)
        {
            if (projectInfo != null)
            {
                var ProjectFiles =  Linq.QueryableProjectItems.GetElementsWhere<ProjectItem>(projectInfo.ProjectItems, p => p.Kind == Linq.ProjectItemTypes.PhysicalFile);
                  
                short totalFiles;

                foreach (var item in ProjectFiles.Cast<ProjectItem>())
                {
                    totalFiles = item.FileCount;

                    for (short i = 1; i <= totalFiles; i++)
                    {
                        if (string.Equals(filePath, item.get_FileNames(i), StringComparison.InvariantCultureIgnoreCase))
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
    }
}
